/*
 * Module.java
 *
 * Created on 05. Juni 2007, 04:12
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sbc.repos;

import sbc.util.*;
import java.util.*;
import org.xvsm.api.core.*;
import corso.lang.*;
import corso.lang.*;

/**
 *
 * @author mustermark
 */
public abstract class Module implements Identifiable, CorsoShareable {
    public enum Type { 
        BOARD, SENSOR, UNIT; 
        public String toString() { 
            if(this == BOARD) return "Board";
            else if(this == SENSOR) return "Sensor"; 
            else return "Unit";
        }
    }
    
    public Module() {}
    
    public Module(CorsoVarOid varOid) {
        //this();
        try
        {
            System.out.println ("Einlesen der Flughafen Info");
            varOid.readShareable (this, null, CorsoConnection.NO_TIMEOUT);
        }
        catch (Exception e)
        {
            System.out.println(e);
            System.exit (-5);
        }
    }
    
    public abstract Tuple asTuple(); 
    public abstract Type getType();
    
    public static Module create(String type, CorsoData data, int arity) throws CorsoException {
        if(type.equals(Type.BOARD.toString())) {
            return new Board(data.getString());
        } else if(type.equals(Type.SENSOR.toString())) {
            String id = data.getString();
            EnumSet<Feature> features = EnumSet.noneOf(Feature.class);
            for(int i = 0; i < arity; i++) features.add(Feature.valueOf(data.getString().toUpperCase()));
            return new Sensor(id, features);
        } else if(type.equals(Type.UNIT.toString())) { 
            String id = data.getString();
            Board b = new Board();
            Sensor s1 = new Sensor();
            Sensor s2 = new Sensor();
            data.getShareable(b);
            data.getShareable(s1);
            data.getShareable(s2);
            return new Unit(b, s1, s2);
            
        }
        return null;
    }
    
}
